package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_UNION_WAR_CHANGE_DISPATCH_SEQUENCE,desc = "调整出战顺序")
public class UnionWarChangeDispatchSequenceCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceReqMsg reqMsg = CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceReqMsg.parseFrom(packet.getBytes());

        UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(reqMsg.getActivityId(), player.getUserId());
        if(unionData == null){
            ret = GameErrorCode.E_UNION_ACTIVITY_GROUP_NO_ABLE;
            return;
        }
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWar(reqMsg.getActivityId());
        if(unionWar == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        UnionWarDispatchDetail detail = unionWar.getUnionWarDispatchDetailList(unionData.getUnionUid(), reqMsg.getPhase(), reqMsg.getSession());
        if(detail == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
        //是否未开战
        if(currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.ShowTime){
            ret = GameErrorCode.E_UNION_WAR_ACTIVITY_OVER_PREPARE_TIME;
            return;
        }
        UnionQunyingTimeInfo timeInfo = currentTimeInfo.getTimeInfo();
        if(timeInfo != null){
            if(timeInfo.getType() > reqMsg.getPhase()){
                ret = GameErrorCode.E_UNION_WAR_ACTIVITY_OVER_PREPARE_TIME;
                return;
            }
            if(timeInfo.getSession() > reqMsg.getSession()){
                ret = GameErrorCode.E_UNION_WAR_ACTIVITY_OVER_PREPARE_TIME;
                return;
            }
            if(timeInfo.getSession() == reqMsg.getSession() && timeInfo.getType() == reqMsg.getPhase() && currentTimeInfo.getPeriodType() == eTimeInfoPeriodType.FightingTime){
                ret = GameErrorCode.E_UNION_WAR_ACTIVITY_OVER_PREPARE_TIME;
                return;
            }

            //直接上跨服调整出战时间
            player.sendUnionActivityGroupPacket(Protocol.C_UNION_WAR_CHANGE_DISPATCH_SEQUENCE, reqMsg.toBuilder(), unionData.getActivityId());
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_UNION_WAR_CHANGE_DISPATCH_SEQUENCE, CrossUnionWarProto.CrossUnionWarChangeDispatchSequenceRespMsg.newBuilder().setRet(ret));
        }
    }
}
